/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao.erp;

import cn.ekuma.data.dao.ViewDAO;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.QBFBuilder;
import com.openbravo.data.loader.SerializerRead;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.bean.erp.viewbean.ProductTransaction;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class ProductTransactionDAO extends ViewDAO<ProductTransaction>{

    public ProductTransactionDAO(I_Session s) {
        super(s);
    }

    public ProductTransaction readValues(DataRead dr,ProductTransaction p) throws BasicException {
        if(p==null)
        	p=new ProductTransaction();
        p.productId=dr.getString(1);
        p.orderTypeName=dr.getString(2);
        p.orderType=dr.getInt(3);
        p.multiply=dr.getDouble(4);
        p.total=dr.getDouble(5);
        return p;
    }

      public List<ProductTransaction> productTransactionListQBF(Object filter) throws BasicException{
        return new StaticSentence(s, new QBFBuilder(
                "SELECT  PS.ID,E.NAME, T.ORDERTYPE, SUM(L.UNITS), SUM(L.UNITS*L.PRICE)  "
                + "FROM ORDERLINES L LEFT  JOIN ORDERS T ON L.ORDERRID = T.ID "
                + "LEFT OUTER JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID "
                + "LEFT OUTER JOIN PRODUCTS PS ON L.PRODUCT = PS.ID "
                + "LEFT OUTER JOIN LOCATION_LAYOUT_CURRENT LLC ON L.PRODUCT = LLC.PRODUCT  "
                + "WHERE L.PRODUCT IS NOT NULL AND ?(QBF_FILTER)  "
                + "GROUP BY PS.ID , E.NAME, T.ORDERTYPE  ",
                new String[]{"T.CURDATE", "T.CURDATE", "T.CUSTOMER", "T.LOCATIONREF", "T.DEFINETYPEREF", "T.PERSON", "L.PRODUCT", "PS.CATEGORY","LLC.LOCATIONLAYOUT"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.STRING}), this).list(filter);
    }

    public  List<ProductTransaction> productPOSTransactionListQBF(Object filter) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
                "SELECT  PS.ID , T.TICKETTYPE, SUM(L.UNITS), SUM(L.UNITS*L.PRICE)  "
                + "FROM TICKETLINES L LEFT JOIN TICKETS T ON L.TICKET = T.ID "
                + "LEFT OUTER JOIN PRODUCTS PS ON L.PRODUCT = PS.ID "
                + "LEFT OUTER JOIN LOCATION_LAYOUT_CURRENT LLC ON L.PRODUCT = LLC.PRODUCT  "
                + "WHERE L.PRODUCT IS NOT NULL AND ?(QBF_FILTER) "
                + "GROUP BY PS.ID, T.TICKETTYPE ", new String[]{"T.CURDATE", "T.CURDATE", "T.CUSTOMER", "T.LOCATIONREF", "T.PERSON", "L.PRODUCT", "PS.CATEGORY","LLC.LOCATIONLAYOUT"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.STRING}), new PosTransactionRead()).list(filter);
    }

    @Override
    public Class getSuportClass() {
        return ProductTransaction.class;
    }

    public static class PosTransactionRead implements SerializerRead<ProductTransaction>{

		@Override
		public ProductTransaction readValues(DataRead dr) throws BasicException {
			ProductTransaction p=new ProductTransaction();
	        p.productId=dr.getString(1);
	        p.orderTypeName="POS";
	        p.orderType=dr.getInt(2);
	        p.multiply=dr.getDouble(3);
	        p.total=dr.getDouble(4);
	        return p;
		}
    }

}
